package com.palm.hubo2o.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.StringRes;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.hubo2o.model.Issue;
import com.palm.hubo2o.util.BallUtils;
import com.palm.hubo2o.util.EnumCode;
import com.palm.palmutils.LayoutUtils;
import com.palm.palmutils.TimeUtils;
import com.palm.palmutils.widget.BallView;
import com.palm.palmutils.widget.ForegroundLinearLayout;

import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by wongqi on 16/6/17.
 */
public class PrizeView extends ForegroundLinearLayout {
    private TextView name;
    private TextView issue;
    private TextView date;
    private LinearLayoutCompat group;
    private boolean ballsAdded;

    public PrizeView(Context context) {
        super(context);
        init(context);
    }

    public PrizeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PrizeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_prize_view, this);
        setOrientation(VERTICAL);
        setBackgroundColor(Color.WHITE);
        final int padding = LayoutUtils.dp2pix(context, 16);
        setPadding(padding, padding, padding, padding);

        name = (TextView) findViewById(R.id.name_prize);
        issue = (TextView) findViewById(R.id.issue_prize);
        date = (TextView) findViewById(R.id.date_prize);
        group = (LinearLayoutCompat) findViewById(R.id.group_balls);
    }

    public void createBalls(EnumCode.gameId id) {
        BallUtils.createBallGroup(group, id);
        ballsAdded = true;
    }

    public void createBalls(List<String> reds, List<String> blues) {
        BallUtils.addBallGroups(group, reds, blues);
        ballsAdded = true;
    }

    public void setBallsChecked(boolean checked) {
        for (int i = 0; i < group.getChildCount(); i++) {
            ((BallView) group.getChildAt(i)).setChecked(checked);
        }
    }

    public void setBalls(Issue issue) {
        if (ballsAdded) {
            List<String> balls = issue.prizeBalls.getRed();
            if (balls != null) {
                if (issue.prizeBalls.getBlue() != null)
                    balls.addAll(issue.prizeBalls.getBlue());
                for (int i = 0; i < Math.min(group.getChildCount(), balls.size()); i++) {
                    ((BallView) group.getChildAt(i)).setNumber(balls.get(i));
                }
            }
        } else {
            BallUtils.addBallGroups(group, issue.prizeBalls.getRed(), issue.prizeBalls.getBlue());
            ballsAdded = true;
        }
    }

    public boolean isBallsAdded() {
        return ballsAdded;
    }

    public void setName(String name) {
        this.name.setText(name);
    }

    public void setName(@StringRes int name) {
        this.name.setText(name);
    }

    public void setIssue(long issue) {
        this.issue.setText(String.format(Locale.getDefault(), "第%d期", issue));
    }

    public void setDate(Date date) {
        this.date.setText(TimeUtils.formatTime(TimeUtils.DEFAULT_FULL_FORMAT, date));

    }

    public void showName(boolean show) {
        this.name.setVisibility(show ? VISIBLE : GONE);
    }
}
